package com.ricky.chat.common.coder;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ricky.chat.common.dto.JsonMsgDto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class JsonMsgDecoder extends MessageToMessageDecoder<String> {
    private final static Logger LOGGER = LoggerFactory.getLogger(JsonMsgDecoder.class);
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void decode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
        LOGGER.info("收到服务端原始数据: {}", msg);
        JsonMsgDto jsonMsgDto = objectMapper.readValue(msg, JsonMsgDto.class);
        out.add(jsonMsgDto);
    }
}